﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using PMC;
using PMC.Plugins;
using PMC.Scripts;
using PMC.Structs;
using System.Linq;
using PMC.Properties;

namespace PMCgui
{
    public partial class WebsitesTab : UserControl
    {
        private List<Script> plugins = Plugin<Script>.Plugins.Instances().ToList();
        private DatagridPanel<Website> websites;
        private IList<Website> websiteObjects = Website.Websites; 

        public WebsitesTab()
        {
            Dock = DockStyle.Fill;
            InitializeComponent();

            cbStandardScript.DataSource = plugins;
            cbStandardScript.SelectedItem = Script.Default;

            websites = new DatagridPanel<Website>(websiteObjects, new[] {"Name", "Webpage", "Username", "Password", "Script"}) {Dock = DockStyle.Fill};
            websites.SetCellType("Script", new DataGridViewComboBoxColumn(), DataGridPanelObjectWrapper<Script>.WrapObjects(plugins));
            websites.SetCellType("Password", new DataGridViewMaskedTextBoxColumn(websites.Datagridview));
            websites.Dock = DockStyle.Fill;
            pnlWebsites.Controls.Add(websites);
        }

        //void Datagridview_DoubleClick(object sender, EventArgs e)
        //{
        //    if (websites.Datagridview.SelectedRows.Count > 0)
        //        new WebsiteForm((Website)websites.Datagridview.SelectedRows[0].DataBoundItem).ShowDialog();
        //    websites.Refresh(); // Notify
        //}

        private void cbStandardScript_SelectedIndexChanged(object sender, EventArgs e)
        {
            var curplug = plugins[cbStandardScript.SelectedIndex];
            var current = curplug.GetScriptInfoByCulture(CultureInfo.CurrentCulture); 
            pnlPlugininfo.Controls.Clear();
            pnlPlugininfo.Controls.Add(new PluginInformationPanel(current));
            button1.Enabled = (Script.Default != cbStandardScript.SelectedItem);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Script.Default = (Script)cbStandardScript.SelectedItem;
            button1.Enabled = false;
        }

        private void btnWebsite_Click(object sender, EventArgs e)
        {
            new WebsiteForm().ShowDialog();
            websites.Refresh();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Language.Instance.ConfirmDeleteWebsites, Language.Instance.Delete, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                foreach (DataGridViewRow row in websites.Datagridview.SelectedRows)
                {
                    websites.Datagridview.CurrentCell = null;
                    ((Website)row.DataBoundItem).Delete();
                    //row.DefaultCellStyle.BackColor = Color.Red;
                    row.Height = 0;
                    row.Resizable = DataGridViewTriState.False;
                    row.Selected = false;
                    row.Visible = false;
                    //row.DefaultCellStyle.BackColor = Color.Red;
                }
            }
            // (websites.Datagridview.SelectedCells.Count > 0)
                
            //websites.Refresh();
        }
    }
}
